#include "player.h"
#include "monster.h"
#include "prop.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <DynamicArray.h>
#include <MyString.h>
#include <StdFile.h>

typedef enum Choice
{
    Save = 1,
    Load,
    Back,
    Exit
}decision;


void SaveGame(player *p,DArray *m)
{
    printf(" ——————————————————————————————————————————\n");
    printf("|                保 存 存 档               |\n");
    printf("|——————————————————————————————————————————|\n");
    printf("|                                          |\n");
    printf("|                |———————|                 |\n");
    printf("|                |存 档 1|                 |\n");
    printf("|                |———————|                 |\n");
    printf("|                                          |\n");
    printf("|                |———————|                 |\n");
    printf("|                |存 档 2|                 |\n");
    printf("|                |———————|                 |\n");
    printf("|                                          |\n");
    printf("|                |———————|                 |\n");
    printf("|                |存 档 3|                 |\n");
    printf("|                |———————|                 |\n");
    printf("|                                          |\n");
    printf("|              |———————————|               |\n");
    printf("|              |返 回 游 戏|               |\n");
    printf("|              |———————————|               |\n");
    printf("|                                          |\n");
    printf(" ——————————————————————————————————————————\n");
    int choice1 = 0;
    printf("请选择存档位置:");
    scanf("%d",&choice1);
    while(getchar() != '\n');
    switch (choice1)
    {
    case 1:
        int n;
        FILE *fp;
        char s[255]; 
        MyStrList special = GetLineFromFile("Exist.txt");
        sprintf(s,"/home/lvguanzhong/suqian252/MyTowerGames/存档/Save1-%s.txt",special.StringList[0].string);
        fp=fopen(s,"w");    
        MyStr str = PlayerToString(p);
        WriteToFile(s,str.string,str.len);
        MyStr mstr = MonsterToString(m);
        AppendToFile(s,mstr.string,mstr.len);
        printf("存档成功!\n");
        sleep(1);
        break;
    case 2:
        int n1;
        FILE *fp1;
        char s1[255]; 
        MyStrList special1 = GetLineFromFile("Exist.txt");
        sprintf(s1,"/home/lvguanzhong/suqian252/MyTowerGames/存档/Save2-%s.txt",special1.StringList[0].string);
        fp1 = fopen(s1,"w");
        MyStr str2 = PlayerToString(p);
        WriteToFile(s1,str2.string,str2.len);
        MyStr mstr2 = MonsterToString(m);
        AppendToFile(s1,mstr2.string,mstr2.len);
        printf("存档成功!\n");
        sleep(1);
        break;
    case 3:
        int n2;
        FILE *fp2;
        char s2[255]; 
        MyStrList special2 = GetLineFromFile("Exist.txt");
        sprintf(s2,"/home/lvguanzhong/suqian252/MyTowerGames/存档/Save3-%s.txt",special2.StringList[0].string);
        fp2 = fopen(s2,"w");
        MyStr str3 = PlayerToString(p);
        WriteToFile(s2,str3.string,str3.len);
        MyStr mstr3 = MonsterToString(m);
        AppendToFile(s2,mstr3.string,mstr3.len);
        printf("存档成功\n!");
        sleep(1);
        break;
    case 4:
        printf("返回游戏中,请稍后...\n");
        sleep(1);
        break; 
    default:
        break;
    }
}

void WriteToGame(player *p,DArray *m,char *(*map)[MAPSZIE],MyStrList content)
{
        InitPlayer(p);
        InitDynamicArray(m);
        
        
        sscanf(content.StringList[0].string,"%d %d %d %d %d %d %d %d %d",&p->HP,&p->MAXHP,&p->ATK,&p->DEF,&p->x,&p->y,&p->level,&p->exp,&p->golden);

        if(content.StringList[1].len != 1)
        {
            weapon *w = CreateWeapon(" ",0,0,0);
            ResizeSting(&w->name,40);
            sscanf(content.StringList[1].string,"%s %d %d %d",w->name.string,&w->ATK,&w->count,(int*)&w->effect);
            p->wpn = w;
        }
        
        if(content.StringList[2].len != 1)
        {
            defence *d = CreateDefence(" ",0,0,0);
            ResizeSting(&d->name,40);
            sscanf(content.StringList[2].string,"%s %d %d %d",d->name.string,&d->DEF,&d->count,(int*)&d->effect);
            p->def = d;
        }

        int i = 4;
        for(;content.StringList[i].len != 1; i++)
        {
            enum PropType type;
            weapon *w = CreateWeapon(" ",0,0,0);
            ResizeSting(&w->name,40);
            defence *d = CreateDefence(" ",0,0,0);
            ResizeSting(&d->name,40);
            medicament *med = CreateMedicament(" ",0,0);
            ResizeSting(&med->name,40);
            //type为道具类型
            int Type =  content.StringList[i].string[0] - 48;
            if(Type == Weapon)
            {
                sscanf(content.StringList[i].string,"%d %s %d %d %d",(int*)&type,w->name.string,&w->ATK,(int *)&w->effect,&w->count);
                p->bag.InsertArrayTail(&p->bag,CreateProp(Weapon,CreateWeapon(w->name.string,w->ATK,w->effect,w->count)));
            }
            if(Type == Defence)
            {
                sscanf(content.StringList[i].string,"%d %s %d %d %d",(int*)&type,d->name.string,&d->DEF,(int *)&d->effect,&d->count);
                p->bag.InsertArrayTail(&p->bag,CreateProp(Defence,CreateDefence(d->name.string,d->DEF,d->effect,d->count)));
            }
            if(Type == Medicament)
            {
                sscanf(content.StringList[i].string,"%d %s %d %d",(int*)&type,med->name.string,&med->HP,(int *)&med->effect);
                p->bag.InsertArrayTail(&p->bag,CreateProp(Medicament,CreateMedicament(med->name.string,med->HP,med->effect)));
            }   
        }
        i++;

        for(;content.StringList[i].len != 1; i++)
        {
            monster *ms = LoadCreateMonster(" ",0,0,0,0,0," ",0,0);
            ResizeSting(&ms->name,40);
            char symbol[20] = {0};
            char *tsymbol;
            tsymbol = symbol;
            char temp[10] = "    ";
            sscanf(content.StringList[i].string,"%s %s %d %d %d %d %d %d %d",symbol,ms->name.string,&ms->HP,&ms->x,&ms->y,&ms->ATK,&ms->DEF,&ms->exp,&ms->golden);
            strcat(temp,tsymbol);
            m->InsertArrayTail(m,LoadCreateMonster(ms->name.string,ms->x,ms->y,ms->HP,ms->ATK,ms->DEF,temp,ms->exp,ms->golden));

        }
        InitMap(map);
        SetMonsterPosition(m,map);
}

void LoadGame(player *p,DArray *m,char *(*map)[MAPSZIE])
{
    printf(" ——————————————————————————————————————————\n");
    printf("|                加 载 存 档               |\n");
    printf("|——————————————————————————————————————————|\n");
    printf("|                                          |\n");
    printf("|                |———————|                 |\n");
    printf("|                |存 档 1|                 |\n");
    printf("|                |———————|                 |\n");
    printf("|                                          |\n");
    printf("|                |———————|                 |\n");
    printf("|                |存 档 2|                 |\n");
    printf("|                |———————|                 |\n");
    printf("|                                          |\n");
    printf("|                |———————|                 |\n");
    printf("|                |存 档 3|                 |\n");
    printf("|                |———————|                 |\n");
    printf("|                                          |\n");
    printf("|              |———————————|               |\n");
    printf("|              |自 动 存 档|               |\n");
    printf("|              |———————————|               |\n");
    printf("|                                          |\n");
    printf("|                 |—————|                  |\n");
    printf("|                 |返 回|                  |\n");
    printf("|                 |—————|                  |\n");
    printf("|                                          |\n");
    printf(" ——————————————————————————————————————————\n");
    int choice1 = 0;
    printf("请选择读档位置:");
    scanf("%d",&choice1);
    while(getchar() != '\n');
    switch (choice1)
    {
    case 1: 
        int n;
        FILE *fp;
        char s[255];
        MyStrList special = GetLineFromFile("Exist.txt");
        sprintf(s,"/home/lvguanzhong/suqian252/MyTowerGames/存档/Save1-%s.txt",special.StringList[0].string);   
        if(IsFileExist(s) == true)
        {
            MyStrList content = GetLineFromFile(s);
            WriteToGame(p,m,map,content);
            printf("读档成功!\n");
            sleep(1);
        }
        else
            printf("没有你的存档,请新建一个存档!\n");
            sleep(1);
        
        break;
    case 2:
        int n1;
        FILE *fp1;
        char s1[255]; 
        MyStrList special1 = GetLineFromFile("Exist.txt");
        sprintf(s1,"/home/lvguanzhong/suqian252/MyTowerGames/存档/Save2-%s.txt",special1.StringList[0].string);
        if(IsFileExist(s1) == true)
        {
            MyStrList content1 = GetLineFromFile(s1);
            WriteToGame(p,m,map,content1);
            printf("读档成功!\n");
            sleep(1);
        }
        else
            printf("没有你的存档,请新建一个存档!\n");
            sleep(1);
        
        break;
    case 3:
        int n2;
        FILE *fp2;
        char s2[255]; 
        MyStrList special2 = GetLineFromFile("Exist.txt");
        sprintf(s2,"/home/lvguanzhong/suqian252/MyTowerGames/存档/Save3-%s.txt",special2.StringList[0].string);
        fp2 = fopen(s2,"r");
        MyStrList content2 = GetLineFromFile(s2);
        WriteToGame(p,m,map,content2);
        printf("读档成功!\n");
        sleep(1);
        break;
    case 4:
        int n3;
        FILE *fp3;
        char s3[255]; 
        MyStrList special3 = GetLineFromFile("Exist.txt");
        sprintf(s3,"/home/lvguanzhong/suqian252/MyTowerGames/存档/Auto-%s.txt",special3.StringList[0].string);
        fp3 = fopen(s3,"r");
        MyStrList content3 = GetLineFromFile(s3);
        WriteToGame(p,m,map,content3);
        printf("读档成功!\n");
        sleep(1);
            break;
    case 5:
        printf("返回上一级中,请稍后...\n");
        sleep(1);
            break;
    default:
        break;
    }
    
}

int EscMenu(player *p,DArray *m,char *(*map)[MAPSZIE])
{
    while(1)
    {
        system("clear");
        printf(" ——————————————————————————————————————————\n");
        printf("|                 暂 停 菜 单              |\n");
        printf("|——————————————————————————————————————————|\n");
        printf("|                                          |\n");
        printf("|                |———————————|             |\n");
        printf("|                |保 存 游 戏|             |\n");
        printf("|                |———————————|             |\n");
        printf("|                                          |\n");
        printf("|                |———————————|             |\n");
        printf("|                |加 载 存 档|             |\n");
        printf("|                |———————————|             |\n");
        printf("|                                          |\n");
        printf("|                |———————————|             |\n");
        printf("|                |返 回 游 戏|             |\n");
        printf("|                |———————————|             |\n");
        printf("|                                          |\n");
        printf("|                |———————————|             |\n");
        printf("|                |退 出 游 戏|             |\n");
        printf("|                |———————————|             |\n");
        printf("|                                          |\n");
        printf(" ——————————————————————————————————————————\n");
        int choice = 0;
        printf("请输入你的操作:");
        scanf("%d",&choice);
        while(getchar() != '\n');
        switch(choice)
        {
            case Save:
                system("clear");
                SaveGame(p,m);
                break;
            case Load:
                system("clear");
                LoadGame(p,m,map);
                break;
            case Back:
                return -1;
                break;
            case Exit:
                printf("确认退出游戏?未保存的内容将丢失!(y,n)");
                char choice3 = 0;
                scanf("%c",&choice3);
                while(getchar() != '\n');
                if(choice3 == 'y')
                {
                    printf("正在退出游戏,请稍后...\n");
                    sleep(1);
                    exit(0);
                }  
                if(choice3 == 'n')
                    break;
            default:
                break;
        }
    }
    
}

void InitPlayer(player *p)
{
    p->MAXHP = 20;
    p->HP = 20;
    p->ATK = 4;
    p->DEF = 0;
    p->x = 0;
    p->y = 0;
    //p->symbol[0] = 0;
    strcpy(p->symbol,"    🥷 ");
    //sscanf("    🥷 ","%s",p->symbol);
    //p->symbol = "    🥷 ";
    p->exp = 0;
    p->level = 0;
    p->golden = 20;
    p->wpn = NULL;
    p->def = NULL;
    p->med = NULL;

    // p->wpnid = 0;
    // p->defid = 0;
    
    // InitWeapon(&p->wpn);
    // InitDefence(&p->def);
    // InitMedicament(&p->med);
    
    InitDynamicArray(&p->bag);
}

void PrintPlayer(player *p)
{
    printf("玩家当前状态: Level: %d  %d/%d❤️  %2d⚔️   %2d🛡️   %2d🪙   exp %d\n",p->level,p->HP,p->MAXHP,p->ATK,p->DEF,p->golden,p->exp);
    if(p->wpn != NULL)
        printf("武器:|%s| %d⚔️   耐久:%d/%d 效果%d\n",p->wpn->name.string,p->wpn->ATK,p->wpn->count,p->wpn->Maxcount,p->wpn->effect);
    if(p->def != NULL)
        printf("盔甲:|%s| %d🛡️   耐久:%d/%d 效果%d\n",p->def->name.string,p->def->DEF,p->def->count,p->def->Maxcount,p->def->effect);
}

void PlayerMove(player *p, DArray *ms,char *(*map)[MAPSZIE])
{
    //将原来的位置清空
    map[p->x][p->y] = "    🔘";
    //InitMap(map);

    char choice = 0;
    printf("请选择玩家移动方向(w,a,s,d,p(进入商店🛒),b(打开背包💼),u(暂停菜单)):");
    printf("\n");
    scanf(" %c",&choice);
    while(getchar() != '\n');
    switch(choice)
    {
        case 'w':
            p->x--;
            if(p->x < 0)
                p->x = 0;
            break;
        case 's':
            p->x++;
            if(p->x >= MAPSZIE)
                p->x = MAPSZIE - 1;
            break;
        case 'a':
            p->y--;
            if(p->y < 0)
                p->y = 0;
            break;
        case 'd':
            p->y++;
            if(p->y >=MAPSZIE)
                p->y = MAPSZIE - 1;
            break;
        case 'p':
            ShopInterFace(p);
            break;
        case 'b':
            ShowBag(p);
            break;
        case 'u':
            EscMenu(p,ms,map);
            break;
        default:
            break;
    }
     
    

    //附上新位置
    map[p->x][p->y] = p->symbol;
}

void LevelUp(player *p)
{
    while(p->exp >= (p->level + 1) * 25)
    {
        p->level++;
        p->exp = p->exp - p->level * 25;
        printf("⭐⭐⭐⭐⭐⭐⭐\n");
        printf("恭喜你升级到%d级\n",p->level);
        printf("⭐⭐⭐⭐⭐⭐⭐\n");
        printf("防御力增加%d,血量上限增加%d,血量回满\n",1,10);
        sleep(2);
        p->DEF += 1;
        p->MAXHP += 10;
        p->HP = p->MAXHP;
        if(p->wpn != NULL)
            p->ATK = (p->ATK - p->wpn->ATK) * 1.25 + p->wpn->ATK; 
        system("clear");
    }
}

void ShopInterFace(player *p)
{
    while(1)
    {
        system("clear");
        printf("\n🎉-------------------欢迎来到商店!-------------------🎉\n");
        printf("请选择你要购买的商品:\n");
        printf("1.烧火棍                             (攻击力 +  2)￥ 5\n");
        printf("2.寒霜巨锤           (有概率冻结敌人)(攻击力 +  5)￥10\n");
        printf("3.吸血刀     (攻击敌人会汲取部分血量)(攻击力 +  3)￥10\n");
        printf("4.魔影剑       (攻击有概率再攻击一次)(攻击力 +  4)￥10\n");
        printf("5.铁盔甲                             (防御力 +  1)￥ 5\n");
        printf("6.克苏鲁之甲   (攻击你会返回部分伤害)(防御力 +  3)￥10\n");
        printf("7.伤痕累累的神圣之甲   (防御很高,但看起来随时会破)￥15\n");
        printf("8.小血瓶                             (血  量 +  5)￥ 2\n");
        printf("9.大血瓶                             (血  量 + 10)￥ 4\n");
        printf("-----------------------敬请期待-----------------------\n");
        printf("                                            q.退出商店\n");
        printf("                                        你当前拥有%d🪙\n",p->golden);
        printf("做出你的选择:");
        char choice = 0;
        scanf(" %c",&choice);
        while(getchar() != '\n');

        switch (choice)
        {
        case '1':
            if(p->golden < 5)
            {
                printf("金币不够,购买失败❌\n");
                sleep(1);
                break;
            }
            printf("购买武器成功✔️\n");
            sleep(1);
           p->bag.InsertArrayTail(&p->bag,CreateProp(Weapon,CreateWeapon("烧火棍",2,-1,5)));
           p->golden -= 5;
            break;
        case '2':
            if(p->golden < 10)
            {
                printf("金币不够,购买失败❌\n");
                sleep(1);
                break;
            }
            printf("购买武器成功✔️\n");
            sleep(1);
           p->bag.InsertArrayTail(&p->bag,CreateProp(Weapon,CreateWeapon("寒霜巨锤",5,Frozen,10)));
           p->golden -= 10;
            break;
        case '3':
            if(p->golden < 10)
            {
                printf("金币不够,购买失败❌\n");
                sleep(1);
                break;
            }
            printf("购买武器成功✔️\n");
            sleep(1);
           p->bag.InsertArrayTail(&p->bag,CreateProp(Weapon,CreateWeapon("吸血刀",3,SuckBlood,10)));
           p->golden -= 10;
            break;
        case '4':
            if(p->golden < 10)
            {
                printf("金币不够,购买失败❌\n");
                sleep(1);
                break;
            }
            printf("购买武器成功✔️\n");
            sleep(1);
           p->bag.InsertArrayTail(&p->bag,CreateProp(Weapon,CreateWeapon("魔影剑",4,Combo,10)));
           p->golden -= 10;
            break;    
        case '5':
            if(p->golden < 5)
            {
                printf("金币不够,购买失败❌\n");
                sleep(1);
                break;
            }
            printf("购买盔甲成功✔️\n");
            sleep(1);
           p->bag.InsertArrayTail(&p->bag,CreateProp(Defence,CreateDefence("铁盔甲",1,-1,5)));
           p->golden -= 5;
            break;
        case '6':
            if(p->golden < 10)
            {
                printf("金币不够,购买失败❌\n");
                sleep(1);
                break;
            }
            printf("购买盔甲成功✔️\n");
            sleep(1);
           p->bag.InsertArrayTail(&p->bag,CreateProp(Defence,CreateDefence("克苏鲁之甲",3,Reflex,10)));
           p->golden -= 10;
            break;
        case '7':
            if(p->golden < 15)
            {
                printf("金币不够,购买失败❌\n");
                sleep(1);
                break;
            }
            printf("购买成功✔️\n");
            sleep(1);
            p->bag.InsertArrayTail(&p->bag,CreateProp(Defence,CreateDefence("伤痕累累的神圣之甲",20,-1,20)));
            p->golden -= 15;
            break;
        case '8':
            if(p->golden < 2)
            {
                printf("金币不够,购买失败❌\n");
                sleep(1);
                break;
            }
            printf("购买成功✔️\n");
            sleep(1);
            p->bag.InsertArrayTail(&p->bag,CreateProp(Medicament,CreateMedicament("小血瓶",5,-1)));
            p->golden -= 2;
            break;
        case '9':
            if(p->golden < 4)
            {
                printf("金币不够,购买失败❌\n");
                sleep(1);
                break;
            }
            printf("购买成功✔️\n");
            sleep(1);
            p->bag.InsertArrayTail(&p->bag,CreateProp(Medicament,CreateMedicament("大血瓶",10,-1)));
            p->golden -= 4;
            break;
        
        case 'q':
            printf("欢迎下次光临!\n");
            sleep(1);
            return;
        default:
            break;
        }
    } 
}

static int count = 0;

void ShowProp(void *ptr)
{
    count++;
    prop *p = (prop*)ptr;
    if(p->type == Weapon)
    {
        weapon *w = (weapon*)p->ptr;
        printf("%d.  武器名字:|%s|  %d⚔️  耐久:%d/%d\n",count,w->name.string,w->ATK,w->count,w->Maxcount);
    }
    if(p->type == Defence)
    {
        defence *d = (defence*)p->ptr;
        printf("%d.  盔甲名字:|%s|  %d🛡️  耐久:%d/%d\n",count,d->name.string,d->DEF,d->count,d->Maxcount);
    }
    if(p->type == Medicament)
    {
        medicament *drug = (medicament*)p->ptr;
        printf("%d.  药水名字:|%s|  增加 %d❤️\n",count,drug->name.string,drug->HP);
    }
}


bool SortByPropType(void *ptr1,void *ptr2)
{
    prop *p1 = (prop*)ptr1;
    prop *p2 = (prop*)ptr2;
    if(p1->type > p2->type)
        return true;
    else 
        return false;
}

int GetInput()
{
    int choice = 0;
    scanf("%d",&choice);
    while(getchar() != '\n');
    return choice;
}


//使用道具
void UseProp(player *p,int index)
{
    if(index < 0 || index > p->bag.len)
    {
        printf("输入非法!请重新输入!\n");
        return;
    }
    //第n个道具的指针
    prop *pro = p->bag.dp[index - 1];
    if(pro->type == Weapon)
    {
        weapon *w = (weapon*)pro->ptr;
        printf("你是否要装备|%s|(%d⚔️ )\n",w->name.string,w->ATK);
        printf("1.是   2.否\n");
        int choice = GetInput();
        if(choice == 1)
        {
            if(p->wpn == NULL)
            {
                p->wpn = w;
                p->ATK += w->ATK;
                printf("装备成功!增加了%d⚔️\n",w->ATK);
                sleep(1);
            } 
            else
            {
                char choice1 = 0;
                printf("已装备武器,是否替换?(y,n)");
                scanf(" %c",&choice1);
                while(getchar() != '\n');
                switch(choice1)
                {
                    case 'y':
                        p->ATK -= p->wpn->ATK;
                        int last = p->wpn->ATK;
                        p->bag.InsertArrayTail(&p->bag,CreateProp(Weapon,p->wpn));
                        p->wpn = w;
                        int now = w->ATK;
                        p->ATK += w->ATK;
                        printf("卸下武器|%s|,装备新武器|%s|",p->wpn->name.string,w->name.string);
                        if(last < now) 
                            printf(" 增加了%d⚔️\n",now-last);
                        if(last > now)
                            printf(" 减少了%d⚔️\n",last-now);
                        sleep(1);
                        break;
                    case 'n':
                        p->bag.InsertArrayTail(&p->bag,CreateProp(Weapon,w));
                    default:
                        break;
                }
                
                
            }
            
            p->bag.RemoveByIndex(&p->bag,index - 1,NULL);
            //释放道具
            free(pro);
        }
        else 
            return;
    }
    if(pro->type == Defence)
    {
        defence *d = (defence*)pro->ptr;
        printf("你是否要装备|%s|(%d🛡️ )\n",d->name.string,d->DEF);
        printf("1.是   2.否\n");
        int choice = GetInput();
        if(choice == 1)
        {
            if(p->def == NULL)
            {
                p->def = d;
                p->DEF += d->DEF;
                printf("装备成功!增加了%d🛡️\n",d->DEF);
                sleep(1);
            }
        else
            {
                char choice1 = 0;
                printf("已装备盔甲,是否替换?(y,n)");
                scanf(" %c",&choice1);
                while(getchar() != '\n');
                switch(choice1)
                {
                    case 'y':
                        p->DEF -= p->def->DEF;
                        int last = p->def->DEF;
                        p->bag.InsertArrayTail(&p->bag,CreateProp(Defence,p->def));
                        p->def = d;
                        int now = d->DEF;
                        p->DEF += d->DEF;
                        printf("卸下原始盔甲|%s|,装备新盔甲|%s|",p->def->name.string,d->name.string);
                        if(last < now) 
                            printf(" 增加了%d🛡️\n",now-last);
                        if(last > now)
                            printf(" 减少了%d🛡️\n",last-now);
                        sleep(1);    
                        break;
                    case 'n':
                        p->bag.InsertArrayTail(&p->bag,CreateProp(Defence,d));
                    default:
                        break;
                }
                 
            }
            
            p->bag.RemoveByIndex(&p->bag,index - 1,NULL);
            //释放道具
            free(pro);
        }
        else 
            return; 
    }
    if(pro->type == Medicament)
    {
        medicament *durg = (medicament*)pro->ptr;
        printf("你是否要使用|%s|(增加%d❤️ )\n",durg->name.string,durg->HP);
        printf("1.是   2.否\n");
        int choice = GetInput();
        if(choice == 1)
        {
            int temp = 0;
            if(p->HP == p->MAXHP)
            {
                printf("血量已满!使用失败!\n");
                sleep(1);
                return;
            }
                
            if(p->MAXHP-p->HP < durg->HP)
            {
                temp = p->MAXHP - p->HP;
                p->HP += temp;
                printf("使用|%s|成功,增加了%d❤️\n",durg->name.string,temp);
                sleep(1);
            }
            else
            {
                p->HP += durg->HP;
                printf("使用|%s|成功,增加了%d❤️\n",durg->name.string,durg->HP);
                sleep(1);
            }  
            if(p->HP > p->MAXHP)
                p->HP = p->MAXHP;
            
            
            p->bag.RemoveByIndex(&p->bag,index - 1,NULL);
            free(pro);
        }
    }
}

void ShowBag(player *p)
{
    while(1)
    {
        p->bag.Sort(&p->bag,SortByPropType);
        system("clear");
        printf("\n--------------------背包----------------------\n");
        count = 0;
        p->bag.DisplayDynamicArray(&p->bag,ShowProp);
        count = 0;
        if(p->bag.len == 0)
        {
            printf("                     无                     \n");
        }
        printf("----------------------------------------------\n");
        printf("                                    q.退出背包\n");
        printf("请选择你的操作:");
        char choice[10] = {0};
        scanf("%s",choice);
        while(getchar() != '\n');
        if(strcmp(choice,"q") == 0)
            return;
        else
            UseProp(p,atoi(choice));

    }  
}

MyStr PlayerToString(player *p)
{
    char str[2048] = {0};
    sprintf(str,"%d %d %d %d %d %d %d %d %d",p->HP,p->MAXHP,p->ATK,p->DEF,p->x,p->y,p->level,p->exp,p->golden);
    strcat(str," ");
    strcat(str,p->symbol);
    strcat(str,"\n");
    //printf("%s\n",str);
    if(p->wpn != NULL)
    {
        char wpn[100] = {0};
        sprintf(wpn,"%s %d %d %d\n",p->wpn->name.string,p->wpn->ATK,p->wpn->count,p->wpn->effect);
        strcat(str,wpn);
    }
    if(p->wpn == NULL)
    {
        strcat(str,".\n");
    }
    if(p->def != NULL)
    {
        char def[100] = {0};
        sprintf(def,"%s %d %d %d\n",p->def->name.string,p->def->DEF,p->def->count,p->def->effect);
        strcat(str,def);
    }
    if(p->def == NULL)
    {
        strcat(str,".\n");
    }

    strcat(str,"背包:\n");
    for(int i = 0; i < p->bag.len; i++)
    {
        char prp[100] = {0};
        prop *pro = p->bag.dp[i];
        if(pro->type == Weapon)
        {
            MyStr s = WeaponToString((weapon*)pro->ptr);
            strcat(str,s.string);
            ClearString(&s);
        }
        if(pro->type == Defence)
        {
            MyStr s = DefenceToString((defence*)pro->ptr);
            strcat(str,s.string);
            ClearString(&s);
        }
        if(pro->type == Medicament)
        {
            MyStr s = MedicamentToString((medicament*)pro->ptr);
            strcat(str,s.string);
            ClearString(&s);
        }
        
    }
    strcat(str,".\n");
    return GetString(str);
}
